﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using FY13.Data;
using System.Linq;

namespace FY13
{
    public class GloabalData : INotifyPropertyChanged
    {
        public static void GoUserInfo()
        {
            G.Navigate.NavigationService.Navigate("/View/UserInfoPage.xaml", null);
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnNotify(string name)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }

        private static GloabalData instance;
        public static GloabalData Instance
        {
            get
            {
                return instance;
            }
        }

        public GloabalData()
        {
            if (instance != null)
                throw new Exception("只能有一个实例");//之所以这么做，是因为需要配合绑定
            instance = this;
        }

        //private const string IsAutoLogin_string = "IsAutoLogin";
        //private bool? isAutoLogin;
        ///// <summary>
        ///// 是否自动登录
        ///// </summary>
        //public bool IsAutoLogin
        //{
        //    get
        //    {
        //        if (isAutoLogin == null)
        //        {
        //            if (System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.Contains(IsAutoLogin_string))
        //            {
        //                isAutoLogin = (bool)System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings[IsAutoLogin_string];
        //            }
        //            else
        //                isAutoLogin = false;
        //        }
        //        return isAutoLogin.Value;
        //    }
        //    private set
        //    {
        //        if (value != isAutoLogin)
        //        {
        //            isAutoLogin = value;
        //            System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings[IsAutoLogin_string] = value;
        //            System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.Save();
        //        }
        //    }
        //}
        private const string AutoLoginId_string = "AutoLoginEmail";
        public string AutoLoginId
        {
            get
            {
                if (System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.Contains(AutoLoginId_string))
                {
                    return (string)System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings[AutoLoginId_string];
                }
                return null;
            }
            set
            {
                if (value == null)
                    System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.Remove(AutoLoginId_string);
                else
                {
                    System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings[AutoLoginId_string] = value;
                }
                System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private Data.UserInfo currentUser;
        public Data.UserInfo CurrentUser
        {
            get
            {
                if (currentUser == null)
                {
                    if (System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.Contains(App.CurrentUser_string))
                    {
                        string str = System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings[App.CurrentUser_string] as string;
                        currentUser = FYDataBase.Users.FirstOrDefault(c => c.Email == str);
                    }
                }
                return currentUser;
            }
            set
            {
                currentUser = value;
                System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings[App.CurrentUser_string] = currentUser.Email;
                System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }


        FYDataBase db;
        public FYDataBase FYDataBase
        {
            get
            {
                if (db == null)
                    db = new FYDataBase();
                return db;
            }
        }
    }
}
